export function html2Text(val) {
  const div = document.createElement('div')
  div.innerHTML = val
  return div.textContent || div.innerText
}

export function dateFormat(time, cFormat) {

}
export function userTypeName(type) {
  return ['普通用户', 'vip用户', '新渠道用户'][parseInt(type)]
}
export function absUrl(url) {
  if (!url) {
    return url;
  }
  if (url.indexOf('://') != -1 || url.indexOf('//') === 0) {
    return url;
  }
  if (url[0] != '/') {
    url = '/' + url;
  }
  if (url.indexOf('/images/') != -1) {
    return url;
  }
  if (url.indexOf('/oss-image/') != -1) {
    return 'http://weixin-ostorage01.oss-cn-shenzhen.aliyuncs.com' + url;
  }
  return 'https://weixina01.bppc.com.cn/zy-admin/UploadFiles/zy/product' + url;
}

export function parseContentImgUrl(content) {
  let domain = "http://weixin-ostorage01.oss-cn-shenzhen.aliyuncs.com";
  content = content.replace(/<img[\s\S]+?src="([^"]+)"/ig, function (m, m1) {
    if (m1 && m1[0] != '/') {
      m1 = '/' + m1;
    }
    let rt = m.replace(m1, domain + m1);
    return rt;
  });
  return content;
}

export function orderStatus(status) {
  let data = {
    "0": "待付款",
    "1": "待发货",
    "2": "待收货",
    "4": "待评论",
    "8": "已评论",
    "16": "已退款",
    "32": "退款处理中",
    "64": "退款失败",
    "99": "已取消",
    "-1": "已取消"
  };
  return data[status + ''];
}
export function moneyFormat(money) {
  return Math.round(money * 100) / 100;
}
export function emptyText(str) {
  return str || '--';
}
export function paymentTypeName(type) {
  return '微信支付';
}

export function parseTime(time, cFormat) {
  if (!time) {
    return time;
  }
  if (arguments.length === 0) {
    return null
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }

  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    date = new Date(parseInt(time))
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function goodsStatus(status) {
  if (status == -1) {
    return ''
  }
  let map = {
    '0': '待支付',
    '1': '待发货',
    '2': '待收货',
    '4': '待评论',
    '8': '已评论',
    '16': '已退款',
    "-1": "已取消",
    '32': '退款中',
    '64': '退款失败',
    '128': '申请退款中',
    '256': '退款拒绝',
  };
  return map[status + ''];
}
